//
//  TTBaseServerTool.m
//  WinesMall
//
//  Created by Isaac on 16/8/27.
//  Copyright © 2016年 Isaac. All rights reserved.
//

#import "TTBaseServerTool.h"
#import "TTHTTPTool.h"
#import "MJExtension.h"

@implementation TTBaseServerTool
#pragma GET请求获取字典
+ (void)getDictWithUrl:(NSString *)url param:(NSMutableDictionary *)param isNeedToken:(BOOL)isNeedToken resultClass:(Class)resultClass complete:(void (^)(BOOL, id, NSError *))complete{
//    __weak typeof(self) weakself = self;
    [TTHTTPTool get:url params:param isNeedToken:isNeedToken complete:^(BOOL network, id responseObj, NSError *error) {
        if (responseObj == nil) {
            if (complete) complete(network, responseObj, error);
        }else{
//            NSLog(@"respj(GET)   : %@",responseObj);
            id result = [resultClass mj_objectWithKeyValues:responseObj];
            NSInteger retCode = [responseObj[@"code"] integerValue];
            
            switch (retCode) {
                    
                case kInvalidCode:{
                    [SVProgressHUD showErrorWithStatus:@"你的账户登录已过期"];
                    [[WSMTools wsmTool] pointToLoginPage];
                    break;
                }
                case kKickedOutCode:{
                    [SVProgressHUD showErrorWithStatus:@"你的账户在其它地方已登录"];
                    [[WSMTools wsmTool] pointToLoginPage];

                    break;
                }
                default:{

                    break;
                }
            }
            if (complete) complete(network, result, error);
        }
    }];
    
}
#pragma GET请求获取数组
+ (void)getArrayWithUrl:(NSString *)url param:(NSMutableDictionary *)param isNeedToken:(BOOL)isNeedToken resultClass:(Class)resultClass complete:(void (^)(BOOL, id, NSError *))complete{
    
    [TTHTTPTool get:url params:param isNeedToken:isNeedToken complete:^(BOOL network, id responseObj, NSError *error) {
        if (responseObj == nil) {
            if (complete) complete(network, responseObj, error);
        }else{
//            NSLog(@"responseObj : %@",responseObj);
            id result = [resultClass mj_objectWithKeyValues:responseObj];
            NSInteger retCode = [responseObj[@"code"] integerValue];
//            NSLog(@"result:     %@",result);
            switch (retCode) {
                    
                case kInvalidCode:{
                    [SVProgressHUD showErrorWithStatus:@"你的账户登录已过期"];
                    [[WSMTools wsmTool] pointToLoginPage];
                    //                    [[ZclTabBarModel sharedMyTabBarModel] presentLoginVC];
                    break;
                }
                case kKickedOutCode:{
                    [SVProgressHUD showErrorWithStatus:@"你的账户在其它地方已登录"];
                    [[WSMTools wsmTool] pointToLoginPage];
                    
                    //                    [[ZclTabBarModel sharedMyTabBarModel] presentLoginVC];
                    break;
                }
                default:{
                    
                    break;
                }
            }
            
            if (complete) complete(network, result, error);
        }
    }];
    
}
#pragma POST请求获取字典
+ (void)postDictWithUrl:(NSString *)url param:(NSMutableDictionary *)param isNeedToken:(BOOL)isNeedToken resultClass:(Class)resultClass complete:(void (^)(BOOL, id, NSError *))complete
{
 
    [TTHTTPTool post:url params:param isNeedToken:isNeedToken complete:^(BOOL network, id responseObj, NSError *error) {
        if (responseObj == nil) {
            if (complete) complete(network, responseObj, error);
        }else{
            
            id result = [resultClass mj_objectWithKeyValues:responseObj];
            ///判断返回编码是否为未登录
            
            NSInteger retCode = [responseObj[@"code"] integerValue];
            
            switch (retCode) {
                    
                case kInvalidCode:{
                    [SVProgressHUD showErrorWithStatus:@"你的账户登录已过期"];
                    [[WSMTools wsmTool] pointToLoginPage];
                    //                    [[ZclTabBarModel sharedMyTabBarModel] presentLoginVC];
                    break;
                }
                case kKickedOutCode:{
                    [SVProgressHUD showErrorWithStatus:@"你的账户在其它地方已登录"];
                    [[WSMTools wsmTool] pointToLoginPage];
                    
                    //                    [[ZclTabBarModel sharedMyTabBarModel] presentLoginVC];
                    break;
                }
                default:{
                    
                    break;
                }
            }
            
            if (complete) complete(network, result, error);
        }
    }];
    
}

#pragma POST请求获取数组
+ (void)postArrayWithUrl:(NSString *)url param:(NSMutableDictionary *)param isNeedToken:(BOOL)isNeedToken resultClass:(Class)resultClass complete:(void (^)(BOOL, id, NSError *))complete
{
    
    [TTHTTPTool post:url params:param isNeedToken:isNeedToken complete:^(BOOL network, id responseObj, NSError *error) {
        if (responseObj == nil) {
            if (complete) complete(network, responseObj, error);
        }else{
//                        NSLog(@"Arr_repj : %@",responseObj);
            id result = [resultClass mj_objectWithKeyValues:responseObj];
            NSInteger retCode = [responseObj[@"code"] integerValue];
            
            switch (retCode) {
                    
                case kInvalidCode:{
                    [SVProgressHUD showErrorWithStatus:@"你的账户登录已过期"];
                    [[WSMTools wsmTool] pointToLoginPage];
                    //                    [[ZclTabBarModel sharedMyTabBarModel] presentLoginVC];
                    break;
                }
                case kKickedOutCode:{
                    [SVProgressHUD showErrorWithStatus:@"你的账户在其它地方已登录"];
                    [[WSMTools wsmTool] pointToLoginPage];
                    
                    //                    [[ZclTabBarModel sharedMyTabBarModel] presentLoginVC];
                    break;
                }
                default:{
                    
                    break;
                }
            }
            if (complete) complete(network, result, error);
        }
    }];
    
}

+ (void)uploadDataWithUrl:(NSString *)url param:(NSMutableDictionary *)param data:(NSData *)data resultClass:(Class)resultClass complete:(void (^)(BOOL, id, NSError *))complete
{
    
    [TTHTTPTool uploadImage:url params:param data:data complete:^(BOOL network, id responseObj, NSError *error) {
        if (responseObj == nil) {
            if (complete) complete(network, responseObj, error);
        }else{
            //            NSLog(@"responseObj : %@",responseObj);
            id result = [resultClass mj_objectWithKeyValues:responseObj];
            if (complete) complete(network, result, error);
        }
    }];
    
}

//上传视频
+ (void)uploadVideoDataWithUrl:(NSString *)url param:(NSMutableDictionary *)param data:(NSData *)data resultClass:(Class)resultClass complete:(void (^)(BOOL, id, NSError *))complete{
    
    
    [TTHTTPTool uploadVideo:url params:param data:data complete:^(BOOL network, id responseObj, NSError *error) {
        
        if (responseObj == nil) {
            if (complete) complete(network, responseObj, error);
        }else{
            //            NSLog(@"responseObj : %@",responseObj);
            id result = [resultClass mj_objectWithKeyValues:responseObj];
            if (complete) complete(network, result, error);
        }
        
    }];
    
}
//下载视频
+ (void)videoDownloadWithUrl:(NSString *)url param:(NSMutableDictionary *)param savePath:(NSString *)savePath data:(NSData *)data resultClass:(Class)resultClass complete:(void (^)(BOOL, id, NSError *))complete{
    
    [TTHTTPTool downloadFileWithURL:url params:param savePath:savePath data:data complete:^(BOOL network, id responseObj, NSError * error) {
        
        
        if (responseObj == nil) {
            if (complete) complete(network, responseObj, error);
        }else{
            //            NSLog(@"responseObj : %@",responseObj);
            id result = [resultClass mj_objectWithKeyValues:responseObj];
            if (complete) complete(network, result, error);
        }
        
        
    }];
    
}

#pragma mark 判断是否是不需要登录的url
+ (BOOL)isNoLoginUrl:(NSString *)url{
//    NSArray *arr = @[GetApplyStatus];
//    if ([arr containsObject:url]) {
//        return YES;
//    }
    return NO;
}



@end
